<?php
declare(strict_types = 1);

namespace app\controller;

use app\BaseController;
use think\Request;

class Template extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $fields =  $fields = $request->param(false);

        list($conditions, $page, $limit) = $this->buildTemplateConditions($fields);

        $total = $this->getTemplateLogic()->countTemplate($conditions);
        $templates = $this->getTemplateLogic()->searchTemplates($conditions, [], $page, $limit);

        return $this->createJsonResponse([
            'total' => $total,
            'templates' => $templates
        ]);
    }

    protected function buildTemplateConditions($fields)
    {
        $conditions = [];
        $page = 0;
        $limit = 20;
        if (!empty($fields['title'])) {
            $conditions['titleLike'] = $fields['title'];
        }

        if (!empty($fields['page'])) {
            $page = (int) $fields['page'];
        }

        if (!empty($fields['limit'])) {
            $limit = (int) $fields['limit'];
        }

        return [$conditions, $page, $limit];
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $fields = $request->param(false);
        $template = $this->getTemplateLogic()->createTemplate($fields);
        return json($template);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $template = $this->getTemplateLogic()->getTemplate($id);
        return json($template);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $template = $this->getTemplateLogic()->getTemplate($id);
        return json($template);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $template = $this->getTemplateLogic()->getTemplate($id);

        if (empty($template)) {
            $this->throwHttpException(500, '该模版不存在');
        }

        $this->getTemplateLogic()->updateTemplate($id, $fields = $request->param(false));

        return $this->createJsonResponse([], '更新成功');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $this->getTemplateLogic()->deleteTemplate($id);

        return $this->createJsonResponse([], '删除成功');
    }

    public function pushMessage(Request $request)
    {
        $userIds = $request->param('userIds', []);
        $templateId = $request->param('templateId');
        $isAll = $request->param('isAll');

        if (empty($templateId) || (!$isAll && empty($userIds))) {
            $this->throwHttpException(500, '参数错误');
        }
        $userIds = array_values($userIds);
        if (empty($isAll)) {
            $this->getMessageLogic()->pushMessage($templateId, $userIds);
        } else {
            $this->getMessageLogic()->pushMessageByTemplateId($templateId);
        }

        return $this->createJsonResponse([], '发送成功');
    }

    public function messageCount(Request $request)
    {
        $count = $this->getMessageLogic()->pushMessageCount();

        return $this->createJsonResponse(['count' => $count], '发送成功');
    }

    /**
     * @return \app\logic\Template
     */
    protected function getTemplateLogic()
    {
        return $this->getLogic('Template');
    }

    /**
     * @return \app\logic\Message
     */
    protected function getMessageLogic()
    {
        return $this->getLogic('Message');
    }
}
